package org.example.struct.array2;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Stack;

/**
 * @author liyishan
 * @date 2023/10/30 11:13
 * @apiNote
 */

public class Test00962MaxWidthRamp {


    public static void main(String[] args) {
        int[] nums = {6,0,8,2,1,5};
        System.out.println(test2(nums));
    }

    public static int test3(int[] nums) {
        Stack<Integer> stack = new Stack<>();
        int n = nums.length;
        // 得到A的单调递减栈
        for (int i = 0; i < n; i++) {
            if (stack.isEmpty() || nums[stack.peek()] > nums[i]) {
                stack.add(i);
            }
        }
        int res = 0, i = n - 1;
        while (i > res) {
            while (!stack.isEmpty() && nums[stack.peek()] <= nums[i]) {
                res = Math.max(res, i - stack.pop());
            }
            i--;
        }
        return res;
    }

    public static int test2(int[] nums) {
        int n = nums.length;
        Integer[] b = new Integer[n];
        for (int i = 0; i < n; i++) {
            b[i] = i;
        }
        Arrays.sort(b, Comparator.comparing(i -> ((Integer) nums[i])));
        int min = n;
        int ans = 0;
        for(int i:b){
            ans = Math.max(ans,i-min);
            min = Math.min(min,i);
        }
        return ans;
    }

    public static int test(int[] nums) {
        int n = nums.length;
        int max = 0;
        ok:for(int i = 0; i < n; i++){
            for(int j = i;j<n;j++){
                if(nums[j] >=nums[i]){
                    max = Math.max(max,j-i);
                    if(j == n-1){
                        break ok;
                    }
                }
            }
        }
        return max;
    }
}
